package com.neuedu.oasystem.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import com.neuedu.oasystem.entity.Project;
import com.neuedu.oasystem.service.ProjectService;
import com.neuedu.oasystem.service.impl.ProjectServiceImpl;

public class ProjectView {

	private Scanner scanner;
	private ProjectService projectService;

	public void init() {

		System.out.println("========================================");
		System.out.println("            欢迎使用项目管理                                       ");
		System.out.println("========================================");
		System.out.println("1.查看项目                             ");
		System.out.println("2.新增项目                           ");
		System.out.println("3.修改项目                            ");
		System.out.println("4.删除项目                             ");
		System.out.println("5.返回上一级                        ");
		System.out.println("========================================");;
	}

	public void work() {
		scanner = new Scanner(System.in);
		projectService = new ProjectServiceImpl();

		boolean running = true;
		while (running) {
			init();
			System.out.print("请选择：");
			String choose = scanner.next();
			switch (choose) {
			case "1":
				System.out.println("查看项目信息");
				findAllProject();
				break;

			case "2":
				System.out.println("新增项目信息");
				addProject();
				break;
			case "3":
				System.out.println("修改项目信息");
				editProject();
				break;
			case "4":
				System.out.println("删除项目信息");
				deleteProject();
				break;
			case "5":
				System.out.println("您选择了退出项目管理界面！");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！请重新输入：");
				break;
			}
		}
	}

	private void findAllProject() {
		System.out.println("项目编号\t\t项目名称\t\t客户编号\t项目金额\t项目期限\t项目负责人编号\t项目负责人姓名\t");
		List<Project> list = projectService.findAll();
		for (Project project : list) {
			System.out.println(project.getProjectId() + "\t" + project.getProjectName() + "\t" + project.getCustomerId()
					+ "\t" + project.getProjectAmount() + "\t" + project.getProjectFuration() + "\t"
					+ project.getProjectLeaderId() + "\t" + project.getProjectLeaderName());
		}
	}

	private boolean addProject() {
		boolean bool = false;
		SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
		scanner = new Scanner(System.in);
		Date projectFuration = null;
		System.out.println("请输入新增项目编号：");
		int projectId = scanner.nextInt();
		System.out.println("请输入新增项目名称");
		String projectName = scanner.next();
		System.out.println("请输入客户编号");
		int customerId = scanner.nextInt();
		System.out.println("请输入项目金额");
		double projectAmount = scanner.nextDouble();
		System.out.println("请输入项目期限");
		try {
			projectFuration = sFormat.parse(scanner.next());
		} catch (ParseException e) {
			System.out.println("日期格式错误");
			bool = true;
			return bool;
		}
		System.out.println("请输入项目负责人编号");
		int projectLeaderId = scanner.nextInt();
		System.out.println("请输入项目负责人姓名");
		String projectLeaderName = scanner.next();
		Project project = new Project(projectId, projectName, customerId, projectAmount, projectFuration,
				projectLeaderId, projectLeaderName);
		boolean res = projectService.add(project);
		if (res) {
			System.out.println("新增项目成功");
			bool = false;
		} else {
			System.out.println("新增资产失败");
		}
		return bool;
	}

	private boolean editProject() {
		Date projectFuration = null;
		boolean bool = false;
		SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("请输入要修改项目的编号");
		int projectId = scanner.nextInt();
		System.out.println("请输入新增项目名称");
		String projectName = scanner.next();
		System.out.println("请输入客户编号");
		int customerId = scanner.nextInt();
		System.out.println("请输入项目金额");
		double projectAmount = scanner.nextDouble();
		System.out.println("请输入项目期限");
		try {
			projectFuration = sFormat.parse(scanner.next());
		} catch (ParseException e) {
			System.out.println("日期格式错误");
			bool = true;
			return bool;
		}
		System.out.println("请输入项目负责人编号");
		int projectLeaderId = scanner.nextInt();
		System.out.println("请输入项目负责人姓名");
		String projectLeaderName = scanner.next();
		Project project = new Project(projectId, projectName, customerId, projectAmount, projectFuration,
				projectLeaderId, projectLeaderName);
		boolean res = projectService.edit(project);
		if (res) {
			System.out.println("新增项目成功");
			bool = false;
		} else {
			System.out.println("新增资产失败");
		}
		return bool;

	}

	private void deleteProject() {
		System.out.println("请输入需要删除计划的编号！");
		int projectId = scanner.nextInt();
		boolean re = projectService.deleteById(projectId);
		if (re) {
			System.out.println("删除成功！");
		}
	}
}
